<template>
	<view>
		<u-navbar
			title="上报工单"
			:autoBack="true"
			placeholder
		>
		</u-navbar>
		<scroll-view :scroll-y="true" :style="{height: scrollViewHeight}">
			<u--form labelPosition="left" :model="model" ref="form1">
				<view style="padding: 0 32rpx;background-color: #fff;">
					<u-form-item labelWidth="80" label="工单位置" borderBottom ref="phone">
						<text>{{name}}</text>
					</u-form-item>
					<u-form-item labelWidth="80" label="任务名称" borderBottom ref="refundPrice">
						<text>{{detail.workCategory_dictText}}</text>
					</u-form-item>
					<u-form-item labelWidth="80" label="任务类型" borderBottom ref="phone">
						<text>{{detail.workOrder_dictText}}</text>
					</u-form-item>
					<view style="color: #303133;font-size: 15px;padding-top: 10px;">备注信息</view>
					<u-form-item class="cus-form-item" borderBottom labelWidth="0">
						<u--textarea height="50px" count :maxlength="50" v-model="model.refund.remark" placeholder="请输入备注" border="none"></u--textarea>
					</u-form-item>
					<u-form-item labelWidth="160" label="工单照片,最多3张" ref="img">
					</u-form-item>
					<u-upload
							:fileList="fileList1"
							@afterRead="afterRead"
							@delete="deletePic"
							name="1"
							multiple
							:maxCount="3"
						></u-upload>
				</view>
			</u--form>
		</scroll-view>
		<view class="good-detail-bootom">
			<view style="margin: 0 32rpx;height: 100%;width: calc(100vw - 64rpx);display: flex;align-items: center;justify-content: space-between;">
				<view class="good-detail-bootom-r">
					<u-button @click="submit" :color="appColor" text="提交工单" shape="circle"></u-button>
				</view>
			</view>	
		</view>
	</view>
</template>

<script>
	import {mapState, mapActions} from 'vuex';
	import {getToken} from '@/utils/auth.js';
	import {base_url} from '@/utils/request.js';
	import {adoptWorkComplete} from '@/api/me.js';
	export default {
		data() {
			return {
				dataList: [],
				actions: [],
				showReason: false,
				model: {
					refund: {
						remark: '',
					},
				},
				name: '',
				fileList1: [],
				orderId: undefined
			}
		},
		computed: {
			...mapState(['appInfo', 'detail']),
			appColor() {
				return this.appPrimaryColor
			},
			scrollViewHeight() {
				return this.scrollHeight - 44 - 50 + 'px';
			},
		},
		onLoad({id, name}) {
			this.orderId = id;
			this.name = name;
		},
		methods: {
			// 提交
			submit() {
				let {remark} = this.model.refund;
				remark = uni.$u.trim(remark);
				if(!remark) {
					return this.$cuTip('请输入备注信息');
				}
				if(this.fileList1.length === 0) {
					return this.$cuTip('请上传工单照片');
				}
				let postObj = {
					id: this.orderId,
					remark,
					image: this.fileList1,
				};
				this.$cuLoading().then(() => {
					adoptWorkComplete(postObj).then(res => {
						uni.hideLoading();
						this.$cuTip('提交成功').then(() => {
							uni.navigateBack({
								delta: 1
							})
						});
					}).catch(() => {
						uni.hideLoading();
					})
				})
				console.log(postObj);
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: base_url + '/jeecg-boot/sys/common/upload', // 仅为示例，非真实的接口地址
						header: {
							'content-type': 'application/json',
							'Login-Type': 'MEMBER',
							'X-Access-Token': getToken(),
						},
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(JSON.parse(res.data).message)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #f5f5f5;
	}
	.u-textarea {
		padding: 9px 0!important;
	}
</style>

<style lang="scss" scoped>
	.shop-cart-box {
		background-color: #fff;
		padding: 32rpx;
		display: flex;
		align-items: center;
		.shop-cart-l {
			display: flex;
			align-items: center;
			width: 72rpx;
		}
		.shop-cart-r {
			flex: 1;
			display: flex;
			.shop-cart-r-1 {
				flex: 1;
				.shop-cart-r-1-title {
					line-height: 34rpx;
					font-size: 28rpx;
					color: #000;
					height: 68rpx;
				}
				.shop-cart-r-1-con {
					height: 48rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
				}
			}
		}
	}
	
	.logistics-box {
		padding: 32rpx;
		background-color: #fff;
		.logistics-title {
			display: flex;
			align-items: center;
			font-size: 28rpx;
			line-height: 28rpx;
			.logistics-title-l {
				color: #666;
				padding-right: 8rpx;
			}
			.logistics-title-r {
				color: #333;
			}
		}
	}
	
	
	.good-detail-bootom {
		background-color: #ffffff;
		position: fixed;
		height: 50px;
		width: 100vw;
		bottom: 0;
		left: 0;
		padding-bottom: constant(safe-area-inset-bottom);     /*兼容 iOS<11.2*/
		padding-bottom: env(safe-area-inset-bottom);  /*兼容 iOS>11.2*/
		box-sizing: content-box;
		.good-detail-bootom-l1 {
			display: flex;
			flex-direction: column;
			justify-content: center;
			.good-detail-bootom-l1-t {
				font-size: 28rpx;
				padding-bottom: 4rpx;
			}
			.good-detail-bootom-l1-b {
				.good-detail-bootom-l1-b-1 {
					display: flex;
					align-items: center;
					font-size: 24rpx;
				}
			}
		}
		.good-detail-bootom-r {
			flex: 1;
		}
	}
</style>